package com.et.util;
/**
 * 获取sysconfig配置信息
 */
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 读取配置文件，更新了UTF-8中文乱码问题
 * @author Administrator
 */
public class SysProp {
	static String filename = "sysconfig.ini";
	private static Properties sysPropties;
	static SysProp sysProp = null;
	/**
	 * 获取一个工程
	 * 
	 * @return Properties
	 */
	@SuppressWarnings("static-access")
	public static Properties getProp() {
		if (sysPropties == null) {
			sysPropties = sysProp.getInstance().getProp();
		}
		return sysPropties;
	}

	/**
	 * 在工程文件中获取filename的值
	 * 
	 * @param propName
	 * @return String
	 */
	@SuppressWarnings("static-access")
	public static String getPropValue(String propName) {
		if (sysPropties == null)
			sysPropties = sysProp.getInstance().getProp(filename);
		String value = sysPropties.getProperty(propName);

		return value;
	}

	/**
	 * 在工程文件中获取filename的值，并以整形返回
	 * 
	 * @param propName
	 * @return int
	 */
	@SuppressWarnings("static-access")
	public static int getIntPropValue(String propName) {
		if (sysPropties == null)
			sysPropties = sysProp.getInstance().getProp(filename);
		int result = 0;
		try {
			result = Integer.parseInt(sysPropties.getProperty(propName));
		} catch (NumberFormatException e) {
			System.err.println("SysProp.getIntPropValue(String).NumberFormatException error : " + e);
		}
		return result;
	}

	/**
	 * 在工程文件中获取filename的值，并以布尔型返回
	 * 
	 * @param propName
	 * @return boolean
	 */
	public static boolean getBlnPropValue(String propName) {
		String temp = getPropValue(propName);
		if (temp != null && temp.equals("true"))
			return true;
		else
			return false;
	}

	/**
	 *　获取指定文件下的指定参数的值
	 * 
	 * @param propName
	 * @param fileName
	 * @return String
	 */
	@SuppressWarnings("static-access")
	public static String getPropValue(String propName, String fileName) {
		Properties prop = sysProp.getInstance().getProp(fileName);
		return prop.getProperty(propName);
	}

	/**
	 * 获取一个工程并实例化
	 * 
	 * @return SysProp
	 */
	protected static SysProp getInstance() {
		if (sysProp == null)
			sysProp = new SysProp();
		return sysProp;
	}

	

	/**
	 * 通过文件名来实例化一个工程类。
	 * 
	 * @param filename
	 * @return Properties
	 */
	@SuppressWarnings({ "rawtypes" })
	private Properties getProp(String filename) {
		Properties props;
		InputStream is = getClass().getClassLoader().getResourceAsStream(filename);
		props = new Properties();
		try {
			props.load(is);
			is.close();
			System.out.println("load system parameters... ");
		} catch (Exception e) {
			System.err.println("Can't read the properties file. Make sure "
					+ filename + " is in the CLASSPATH");
			return null;
		}
		Enumeration enu = props.propertyNames();
		String value = "";
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			value = props.getProperty(name);
			try {
				value = new String(value.getBytes("ISO-8859-1"));
			} catch (UnsupportedEncodingException e) {
				System.err.println(getClass().getName() + ".getProp(String) error : " + e);
			}
			props.setProperty(name, value);
		}
		return props;
	}
	
	/**
	 * 过滤html标签
	 * 
	 * @param request
	 * @return
	 */
	public static String Html2Text(String inputString) { 
		String htmlStr = inputString; //含html标签的字符串 
		String textStr =""; 
		Pattern p_script; 
		Matcher m_script; 
		Pattern p_style; 
		Matcher m_style; 
		Pattern p_html; 
		Matcher m_html; 
   
		try { 
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> } 
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> } 
			String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 

			p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
			m_script = p_script.matcher(htmlStr); 
			htmlStr = m_script.replaceAll(""); //过滤script标签 

			p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
			m_style = p_style.matcher(htmlStr); 
			htmlStr = m_style.replaceAll(""); //过滤style标签 

			p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
			m_html = p_html.matcher(htmlStr); 
			htmlStr = m_html.replaceAll(""); //过滤html标签 
      
			textStr = htmlStr; 
      
		}catch(Exception e) { 
			System.err.println("Html2Text: " + e.getMessage()); 
		} 
   
		return textStr;//返回文本字符串 
	}
}
